<?php

// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: Cruise <jiangyu@jjhh.com>
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * QA系统
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2016-03-12
 +------------------------------------------------------------------------------
 */
namespace Home\Controller;

class AskController extends HomeController{
    
    /**
     * @todo 问答首页
     * @author fzh
     */
    public function index(){
        $obj = D('Admin/ServiceApi');

        //文章分类
        $class_list = $obj->getAskCategoryTree();
        $this->assign('class_list',$class_list);
        
        //精华问答
        $position_article_list = $obj->getPositionArticleList(1);
        $this->assign('position_article_list',$position_article_list);
        
        //最新更新文章
        $new_article_list = $obj->getNewArticleList();
        $this->assign('new_article_list',$new_article_list);
        
        //最新标签
        $map['position'] = 1;
        $tags_list = $obj->getAskTags($map,10);
        $this->assign('tags_list',$tags_list);
        $this->display();
    }
    
    /**
     * @todo 标签列表页
     * @author fzh
     */
    public function tags(){
        $obj = D('Admin/ServiceApi');
        $tags_list = $obj->getAskTags();
        $data = array();
        foreach ($tags_list as $key => $val){
            $letter = getFirstCharter($val);
            if(empty($letter)){
                $letter = '其';
            }
            $data[$letter][$key] = $val;
        }
        ksort($data);
        $this->assign('tags_list',$data);
        $this->display();
    }
    
    /**
     * @todo 文章列表页
     * @author fzh
     */
    public function article(){
        $id = (int)I('get.id',0);
        $pid = (int)I('get.pid',0);
        $p = (int)I('get.p',1);
        $obj = D('Admin/ServiceApi');
        $page_num = 10;
        
        //文章一级分类
        $class_list = $obj->getAskCategoryTree();
        $this->assign('class_list',$class_list);
        
        //文章二级分类
        if($pid>0 && isset($class_list[$pid]['_child'])){
            $two_class_list = $class_list[$pid]['_child'];
        }else{
            $two_class_list = null;
        }
        $this->assign('two_class_list',$two_class_list);
        
        //面包屑
        $nav_list['one'] = ($pid>0) ? $obj->getAskCategory($pid) : null;
        $nav_list['two'] = ($id>0) ? $obj->getAskCategory($id) : null;
        $this->assign('nav_list',$nav_list);
       
        //meta
        if(!empty($nav_list['two'])){
            $meta = $nav_list['two'];
        }else{
            $meta = $nav_list['one'];
        }
        $this->assign('meta',$meta);
        
        //大家都在问
        $position_map['position'] = 2;
        $position_map['status'] = 1;
        $qa_list = $obj->getArticleList($position_map, 10);
        $this->assign('qa_list',$qa_list);
        
        //文章总条数
        $map['status'] = 1;
        if($pid>0){
            $map['ask_category_pid'] = $pid;
        }
        if($id>0){
            $map['ask_category_id'] = $id;
        }
        $article_count = M('AskArticle')->where($map)->count();
        
        //文章list
        $article_list = $obj->getArticleList($map,0,$p,$page_num);
        $this->assign("article_list",$article_list);
        
        //分页信息
        $page = new \Think\Page($article_count,$page_num);
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('theme', '<span class="pages"> %FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%</span>');
        $page = $page->show();
        $this->assign("_page", $page);
        
        $this->display();
    }
    
    /**
     * @todo 详情页
     * @since 2016-03-12
     * @author Cruise <jiangyu@jjhh.com>
     */
    public function detail(){
    	$article_id = (int)I('get.id',0);
    	if($article_id == 0){
    		$this->error('文章id错误');
    	}
    	$obj = D('Admin/ServiceApi');
    	$article_obj = D('Ask');
    	//文章详情
    	$article_detail = $article_obj->getArticleInfo($article_id);
    	if($article_detail === false){
    		$this->error($article_obj->getError());
    	}
    	//取当前文章下的五篇文章
    	$map['ask_article_id'] = array('gt',$article_id);
    	$map['status'] = 1;
    	$articel_title_list = $obj->getArticleList($map,5);
    	//热门标签
    	$tags_all = $obj->getAskCategoryTags($article_detail['ask_category_id']);
    	if(!empty($tags_all)){
    		$tags = array_column($tags_all,'ask_tags_id');
    		$map['position'] = 4;
    		$map['ask_tags_id'] = array('in',$tags);
    		$hot_tags = $obj->getAskTags($map,10);
    	}else{
    		$hot_tags = null;
    	}
     	$this->assign('article_detail',$article_detail);
     	$this->assign('articel_title_list',$articel_title_list);
     	$this->assign('hot_tags',$hot_tags);
    	$this->display();
    }
    
    /**
     * @todo search
     * @since 2016-03-12
     * @author Cruise <jiangyu@jjhh.com>
     */
    public function search(){
    	$search_text = trim(strip_tags(I('search_text')));//搜索内容
    	$search_id = (int)I('id',0)>0 ? (int)I('id',0) : 0;
    	$p = (int)I('p')==0?1:(int)I('p');
    	$page_number = 10;
    	$obj = D('Ask');
    	$obj_api = D('Admin/ServiceApi');
    	//面包屑，标签，都在问，内容展示
    	$search_info = $hot_tags = $qa_list = null;
    	if($search_id > 0) {
    		$tags_info = $obj_api->getAskTagsInfo($search_id);
    		if(!empty($tags_info)){
    			$map['ask_article_title'] = array('like', '%'.(string)$tags_info['ask_tags_name'].'%');
    			$search_info = $tags_info['ask_tags_name'];
    			$this->assign('tags_info',$tags_info);
    		}else{
    			header("HTTP/1.1 404 Not Found");die('<center>网页好像私奔了！</center>');
    		}
    		//取出热门标签
    		$map_position['position'] = 8;
    		$hot_tags = $obj_api->getAskTags($map_position,10);
    		//大家都在问
    		$position_map['position'] = 2;
    		$position_map['status'] = 1;
    		$qa_list = $obj_api->getArticleList($position_map, 10);
    	}else{
    		$map['ask_article_title'] = array('like', '%'.(string)$search_text.'%');
    		$search_info = $search_text;
    	}
    	$map['status'] = 1;
    	//特例页不展示热门和标签
     	$different = !empty($search_id) ? true : false;
    	//分页取总
    	$article_count = M('AskArticle')->where($map)->count();
    	//取出文章列表
    	$article_list = $obj->getArticleList($map,0,$p,$page_number);
    	$page = new \Think\Page($article_count,$page_number);
    	$page->setConfig('prev','上一页');
     	$page->setConfig('next','下一页');
      	$page->setConfig('theme', '<span class="pages"> %FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%</span>');
    	$page = $page->show();
    	$this->assign("_page", $page);
    	$this->assign("article_list",$article_list);
     	$this->assign('different',$different);
    	$this->assign('search_text',$search_info);
    	$this->assign('qa_list',$qa_list);
    	$this->assign('hot_tags',$hot_tags);
    	$this->display('list');
    }
    
    
}